from fastapi import APIRouter, Request
from fastapi import Depends
from sqlalchemy.orm import Session

from config.get_db import get_db
from core.entity.vo.common import CrudResponse
from core.entity.vo.view_models import CmsGaodeServiceVO
from core.service.cms_gaode_service_service import CmsGaodeServiceService
from utils.log_util import *
from utils.response_util import *

gaodeServiceController = APIRouter()


@gaodeServiceController.post("/gaode_terminal/query_by_id", response_model=CmsGaodeServiceVO)
async def get_gaode_service_by_id(request: Request, id: int, query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据
        cms_gaode_service = CmsGaodeServiceService.get_cms_gaode_service_by_id(query_db, id)
        logger.info('获取成功')
        return response_200(data=cms_gaode_service, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@gaodeServiceController.post("/gaode_service/query_by_service_id", response_model=CmsGaodeServiceVO)
async def get_gaode_service_by_service_id(request: Request, service_id: str, query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据
        cms_gaode_service = CmsGaodeServiceService.get_cms_gaode_service_by_service_id(query_db, service_id)
        logger.info('获取成功')
        return response_200(data=cms_gaode_service, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@gaodeServiceController.post("/gaode_service/query_like_service_name", response_model=CmsGaodeServiceVO)
async def get_gaode_service_like_service_name(request: Request, service_name: str, query_db: Session = Depends(get_db)):
    try:
        # 获取全量数据
        cms_gaode_service_list = CmsGaodeServiceService.get_cms_gaode_service_like_name(query_db, service_name)
        logger.info('获取成功')
        return response_200(data=cms_gaode_service_list, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@gaodeServiceController.post("/gaode_service/add", response_model=CrudResponse)
async def add_gaode_service(request: Request, cms_gaode_service: CmsGaodeServiceVO, query_db: Session = Depends(get_db)):
    try:
        cms_gaode_service_result = await CmsGaodeServiceService.add_cms_gaode_service(request, query_db,
                                                                                      cms_gaode_service)
        if cms_gaode_service_result.is_success:
            logger.info(cms_gaode_service_result.message)
            return response_200(data=cms_gaode_service_result, message=cms_gaode_service_result.message)
        else:
            logger.warning(cms_gaode_service_result.message)
            return response_400(data="", message=cms_gaode_service_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@gaodeServiceController.patch("/gaode_service/edit", response_model=CrudResponse)
async def edit_gaode_service(request: Request, cms_gaode_service: CmsGaodeServiceVO, query_db: Session = Depends(get_db)):
    try:
        cms_gaode_service_result = await CmsGaodeServiceService.edit_cms_gaode_service(query_db, cms_gaode_service)
        if cms_gaode_service_result.is_success:
            logger.info(cms_gaode_service_result.message)
            return response_200(data=cms_gaode_service_result, message=cms_gaode_service_result.message)
        else:
            logger.warning(cms_gaode_service_result.message)
            return response_400(data="", message=cms_gaode_service_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@gaodeServiceController.post("/gaode_service/delete", response_model=CrudResponse)
async def delete_gaode_service(request: Request, cms_gaode_service: CmsGaodeServiceVO, query_db: Session = Depends(get_db)):
    try:
        delete_gaode_service_result = CmsGaodeServiceService.delete_cms_gaode_service(query_db, cms_gaode_service.id,
                                                                                      cms_gaode_service.service_id)
        if delete_gaode_service_result.is_success:
            logger.info(delete_gaode_service_result.message)
            return response_200(data=delete_gaode_service_result, message=delete_gaode_service_result.message)
        else:
            logger.warning(delete_gaode_service_result.message)
            return response_400(data="", message=delete_gaode_service_result.message)
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))
